<template>
	<view>
		给{{toName}}的留言
	</view>
	<view>
		<HbComment ref="hbComment" @add="add" @del="del" @like="like" @focusOn="focusOn" :deleteTip="'确认删除？'"
		    :cmData="commentData" :topName="toName"></HbComment>
	</view>
	</template>
<script setup>
	import { ref ,onMounted, inject} from 'vue';
    import HbComment from '@/uni_modules/hb-comment/components/hb-comment/hb-comment.vue'
	import { onLoad } from '@dcloudio/uni-app'
	const pinia=inject("$pinia")
	const http=inject("$http")
	onMounted(()=>{
		
	});
	const params={
		page:1,
		size:100
	}
	const resmy=null;
	const toName=ref("");
	const toUserId=ref("")
	const commentData = ref({"readNumer":0,"commentSize":0,"comment":[]});
	const oldrows=ref([]);//后台原始结构
	const oldreadNumber=ref(0);
	const oldcommentSize=ref(0);
	onLoad((options)=>{
		if(options.topicId){
			params.topicId=options.topicId;		
		}else{
			params.topicId=options.toId+"-"+pinia.userId;
		}
		if(options.replyUserId){
			params.replyUserId=options.replyUserId;	
		}else{
			params.replyUserId=pinia.userId;
		}

		toUserId.value=options.toId;
		toName.value=options.toName;
		http.post("/topicMessage/hpGetListByTopicId", params).then((res) => {
			if (res && res.data && res.data.code == 1) {
				let resmy = res.data.data;
				oldrows.value=resmy.commentList;
				oldreadNumber.value=resmy.readNumer;
				oldcommentSize.value=resmy.commentList.length;
				commentData.value={
				    "readNumer": oldreadNumber.value,
				    "commentSize": oldcommentSize.value,
				    "comment": getTree(oldrows.value)
				}
			}
		}).catch((re) => {
			
		});
	})
function add(e){
	params.type=2;
	params.content=e.content;
	params.topId=e.pId;
	params.replyId=e.itemId;
	http.post("/topicMessage/hpAdd", params).then((res) => {
		if (res && res.data && res.data.code == 1) {
			oldrows.value.unshift(res.data.data);
			commentData.value={
			    "readNumer": oldreadNumber.value,
			    "commentSize": oldrows.value.length,
			    "comment": getTree(oldrows.value)
			}
		}
	}).catch((re) => {
		
	});
}
function del(id){
	http.post("/topicMessage/delete", {id:id}).then((res) => {
		if (res && res.data && res.data.code == 1) {
			oldrows.value=oldrows.value.filter(item => item.id!=id);
			oldrows.value=oldrows.value.filter(item => item.parentId!=id);
			commentData.value={
			    "readNumer": oldreadNumber.value,
			    "commentSize": oldrows.value.length,
			    "comment": getTree(oldrows.value)
			}
			console.log(commentData.value)
		}
	}).catch((re) => {
		
	});
}
function getTree(data) {
    let result = [];
    let map = {};
    data.forEach(item => {
        map[item.id] = item;
		item.children=[];
    });
    data.forEach(item => {
        let parent = map[item.parentId];
        if (parent) {
            (parent.children || (parent.children = [])).push(item);
        } else {
            result.push(item);
        }
    });
    return result;
}
</script>

<style lang="scss" scoped>
    .content{
        display: flex;
        align-items: center;
        justify-content: space-around;
    }
</style>